package cn.chinaflame.pagodas.service;

import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.utils.HttpPostUtil;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.SpringProperties;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 项目service
 *
 * @author created by boming
 * @date 2018年1月5日 上午9:40:37
 */
@Service
@EnableAsync
public class CommonService {


    private static final Logger log = LoggerFactory.getLogger(CommonService.class);


    private String productLightSearchAndSend = SpringProperties.getProperty("product.light.search");
    private String productLightClear = SpringProperties.getProperty("product.light.clear");
    @Autowired
    private RestTemplate restTemplate;

    public boolean paginationSupport(Integer pageIndex, Integer pageSize) {
        log.info("判断是否可分页pageIndex:{},pageSize:{}", pageIndex, pageSize);
        if (pageIndex == null && pageSize == null)
            return false;
        ParamPreconditions.checkArgument(pageIndex > 0 && pageSize > 0, CodeEnum.FAIL.getCode(), "页码或页数不能为负数");
        return true;
    }

    /**
     * 寻灯安装灯
     *
     * @param locationNos
     */
    @Async
    public void searchLight(List<String> locationNos, String beWishName) {
        log.info("远程寻灯并点亮locationNos:{}", locationNos);
        JSONArray array = new JSONArray();
	  /*JSONObject obj  = new JSONObject();
		obj.put("lightno", "0101010101");
		obj.put("obj", "硬件1");
		JSONObject obj2 = new JSONObject();
		obj2.put("lightno", "0101010102");
		obj2.put("obj", "硬件2");
		JSONObject obj3 = new JSONObject();
		obj3.put("lightno", "0101010103");
		obj3.put("obj", "硬件3");
		JSONObject obj4 = new JSONObject();
		obj4.put("lightno", "0101010104");
		obj4.put("obj", "硬件4");
		
		array.add(obj);
		array.add(obj2);
		array.add(obj3);
		array.add(obj4);*/
        for (String lightno : locationNos) {
            JSONObject obj = new JSONObject();
            obj.put("lightno", lightno);
            obj.put("obj", beWishName);
            array.add(obj);
        }
        String response = HttpPostUtil.doPost1(productLightSearchAndSend, "lightvo=" + array.toJSONString());
        log.info("远程寻灯返回结果result:{}", response);
    }

    /**
     * 熄灯
     *
     * @param locationNos
     */
    @Async
    public void clearLight(List<String> locationNos) {
        log.info("远程熄灯locationNos:{}", locationNos);
        String lightnos = locationNos.toString();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<String> requestEntity = new HttpEntity<String>("lightnos=" + lightnos, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(productLightClear, HttpMethod.POST, requestEntity, String.class);
        log.info("远程熄灯返回结果result:{}", response);

    }
    public String getInStr(String str){
    	return null;
    }
}
